﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Strive.DataAccess.Base.Model.PageObject;
using Strive.DataAccess.Base.Model.SearcherObject;

using OTA.Notification.BO.Template;
using OTA.Notification.Entity.Template;


using OTA.Notification.BO.Dictionary;
using OTA.Notification.Entity.Dictionary;

using System.Collections.Generic;

namespace OTA.Notification.Web.Template
{
    public partial class TemplateList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        int iState = 1;
                        string strRoom = string.Empty;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        if (!string.IsNullOrEmpty(Request.Params["query"]))
                            ParseQuery(Request.Params["query"], out strRoom, out iState);

                        Response.Write(GetJSON(iPageIndex, iState, strRoom, iPageSize));
                        Response.End();
                    }
                    else if (Request.QueryString["action"].ToLower() == "del")
                    {

                        if (!string.IsNullOrEmpty(Request.Params["key"]))
                        {

                            bool b = DoDelete(Request.Params["key"]);

                            if (b)
                                Response.Write("{state:1}");
                            else
                                Response.Write("{state:0}");

                            Response.End();
                        }
                    }
                }

            }
        }

        private bool DoDelete(string Key)
        {
            try
            {
                int iEnd = Key.LastIndexOf('_');

                string strTemplateCode = Key.Substring(0, iEnd);

                string strMsgTypeCode = Key.Substring(iEnd + 1, Key.Length - iEnd - 1);



                using (TemplateBO bo = new TemplateBO())
                {

                    MsgTemplateConfigBean bean = bo.SelectByPK(strTemplateCode, strMsgTypeCode);// update by kevin 6.25

                    if (bean != null)
                    {
                        bo.Delete();

                        bo.Save(bean);
                    }


                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        private void InitPage()
        {

        }

        /// <summary>
        /// 解析查询
        /// </summary>
        /// <param name="Query"></param>
        /// <param name="Room"></param>
        /// <param name="State"></param>
        private void ParseQuery(string Query, out string Room, out int State)
        {
            string[] sp = Query.Split(',');

            State = -1;
            Room = string.Empty;

            if (sp.Length == 2)
            {
                Room = sp[0];
                int.TryParse(sp[1], out State);
            }
        }

        private string GetJSON(int PageIndex, int State, string Room, int PageSize)
        {

            string strJson = "";

            using (TemplateBO bo = new TemplateBO())
            {

                BasePageSearch bs = new BasePageSearch();

                bs.PageSize = PageSize;
                bs.PageIndex = PageIndex;
                bs.Column = "*";

                bs.Order = "MSG_TEMPLATE_CODE";

                PageResult<MsgTemplateConfigBean> pr = bo.SelectPageList(bs);
                int iViewStatePageCount = 0;

                if (Session["SESSION_PAGE_COUNT"] != null)
                    int.TryParse(Session["SESSION_PAGE_COUNT"].ToString(), out iViewStatePageCount);

                if (pr.RecordCount == 0)
                    pr.RecordCount = iViewStatePageCount;
                else
                    Session["SESSION_PAGE_COUNT"] = pr.RecordCount;

                strJson = "{\"total\":" + pr.RecordCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

                using (DictionaryBO dic = new DictionaryBO())
                {
                    foreach (MsgTemplateConfigBean bean in pr.Data)
                    {

                        strJson += "{\"id\":\"" + HttpUtility.HtmlEncode(bean.TemplateCode) + "_" + bean.SendTypeCode + "\",";

                        //strJson += "\"mscode\":\"" + HttpUtility.HtmlEncode(bean.SendTypeCode) + "\",";
                        strJson += "\"cell\":[";

                        strJson += "\"" + HttpUtility.HtmlEncode(bean.TemplateCode) + "\",";
                        strJson += "\"" + HttpUtility.HtmlEncode(bean.Flag) + "\",";
                        strJson += "\"" + HttpUtility.HtmlEncode(bean.TemplateName) + "\",";

                        strJson += "\"" +
                                    HttpUtility.HtmlEncode(
                                    dic.getName(OTA.Notification.Common.DicType.Message,
                                    "Send", bean.SendTypeCode,
                                    OTA.Notification.Common.LanguageType.Chinese)) + "\","; //发送类型

                        strJson += "\"" + HttpUtility.HtmlEncode(bean.LastUpdateUserName) + "\",";
                        strJson += "\"" + bean.LastUpdateTime.ToString("yyyy-MM-dd") + "\"";

                        strJson += "]},";
                    }
                }

            }

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
